﻿using BadmintonBuddy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Web;
using System.Web.Mvc;

namespace BadmintonBuddy.Controllers
{
    public class Common
    {
        #region Common Methods
        
        #endregion Common Methods

        #region DBHelpers
        public static City GetCity(string cityname, string statename, string countryname)
        {

            City city = null;
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                if (String.IsNullOrEmpty(statename) || String.IsNullOrEmpty(countryname))
                {
                    city = new City();
                    city.TempCityName = cityname;
                    city.StateID = 1; // Make first state as the default assignment                    
                }
                else
                {
                    if (!String.IsNullOrEmpty(cityname.Trim()))
                    {
                        IQueryable<City> cityColl = from db_city in dbContext.Cities
                                                    where db_city.CityName.Trim().ToLower() == cityname.Trim().ToLower()
                                                    select db_city;
                        if (cityColl != null && cityColl.Count() > 0)
                        {
                            city = cityColl.First<City>();
                        }
                        else
                        {
                            city = new City();
                            city.CityName = cityname.Trim();

                            if (!String.IsNullOrEmpty(statename.Trim()))
                            {

                                IQueryable<State> stateColl = from db_state in dbContext.States
                                                              where db_state.StateName.Trim().ToLower() == statename.Trim().ToLower()
                                                              select db_state;
                                if (stateColl != null && stateColl.Count() > 0)
                                {
                                    city.State = stateColl.First<State>();
                                }
                                else
                                {
                                    State newState = new State();
                                    newState.StateName = statename.Trim();
                                    city.State = newState;
                                    if (!String.IsNullOrEmpty(countryname.Trim()))
                                    {
                                        IQueryable<Country> countryColl = from db_country in dbContext.Countries
                                                                          where db_country.CountryName.Trim().ToLower() == countryname.Trim().ToLower()
                                                                          select db_country;

                                        if (countryColl != null && countryColl.Count() > 0)
                                        {
                                            newState.Country = countryColl.First<Country>();
                                        }
                                        else
                                        {
                                            Country newCountry = new Country();
                                            newCountry.CountryName = countryname.Trim();
                                            newState.Country = newCountry;
                                        }
                                    }
                                }
                            }

                        }

                    }
                }
            }
            return city;
        }
        public static UserProfile GetUserFromProviderId(string providerId)
        {
            UserProfile user = null;
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                IQueryable<UserProfile> userColl = from db_user in dbContext.UserProfiles
                                                   join db_membership in dbContext.webpages_OAuthMembership
                                                   on db_user.UserId equals db_membership.UserId
                                                   where db_membership.ProviderUserId.ToLower() == providerId.ToLower()
                                                   select db_user;
                if (userColl.Count() > 0)
                {
                    user = userColl.First<UserProfile>();
                }
            }
            return user;
        }

        public static UserProfile GetUserFromUserId(Int32 userId)
        {
            UserProfile user = null;
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {
                IQueryable<UserProfile> userColl = from db_user in dbContext.UserProfiles
                                                   where db_user.UserId == userId
                                                   select db_user;
                if (userColl.Count() > 0)
                {
                    user = userColl.First<UserProfile>();
                }
            }
            return user;
        }

        public static UserDetail GetUserDetailFromUserId(Int32 userId)
        {
            UserDetail userDetail = null;
            using (BadmintonBuddyEntities dbContext = new BadmintonBuddyEntities())
            {

                IQueryable<UserDetail> userColl = from db_user in dbContext.UserDetails
                                                  where db_user.UserID == userId
                                                  select db_user;
                if (userColl.Count() > 0)
                {
                    userDetail = userColl.First<UserDetail>();
                }
            }
            return userDetail;
        }


        #endregion

        
    }
}